package com.inspur.ssm.dao.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.inspur.ssm.dao.NewsDao;
import com.inspur.ssm.dao.UserDao;
import com.inspur.ssm.pojo.NewsBean;
import com.inspur.ssm.pojo.UserBean;


@Repository("newsDao")
public class NewsDaoImpl implements NewsDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<NewsBean> getNewsList() {

		List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from news ");
		
		List<NewsBean> clist = new ArrayList<NewsBean>();
		if(list!=null&&list.size()>0){
			
			for(Map<String,Object> mapl:list){
				NewsBean newsBean=new NewsBean();

			
	
				newsBean.setId(mapl.get("id").toString());
				newsBean.setTitle(mapl.get("title").toString());
				newsBean.setConetnt(mapl.get("conetnt").toString());
				newsBean.setAuthor(mapl.get("author").toString());
				newsBean.setAddtime((Date)mapl.get("addtime"));

				clist.add(newsBean);
			}
			
		}
		return clist;
	}


	@Override
	public NewsBean getNews(String id) {
		NewsBean newsBean=null;
		Object [] obj=new Object[1];
		obj[0]=id;

		List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from news where id=?",obj);
		if(list!=null&&list.size()>0){
			newsBean=new NewsBean();
			for(Map<String,Object> map:list){
				
				newsBean.setId(map.get("id").toString());
				newsBean.setTitle(map.get("title").toString());
				newsBean.setConetnt(map.get("conetnt").toString());
				newsBean.setAuthor(map.get("author").toString());

				newsBean.setAddtime((Date)map.get("addtime"));
			}
		}
		return newsBean;
	}
	

}
